package com.wenhuitec.android.tang.ui.activity;

import static java.lang.Thread.sleep;

import android.Manifest;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

import com.dhkj.printlib.PrintStart;
import com.dhkj.web.basic.Constans;
import com.lake.banner.transformer.DefaultTransformer;
import com.lake.banner.view.BannerViewPager;
import com.lake.hbanner.HBanner;
import com.lake.hbanner.ImageSubView;
import com.lake.hbanner.SubView;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.config.SystemInit;
import com.wenhuitec.android.tang.ui.dialog.PrintSetDialog;
import com.wenhuitec.android.tang.ui.dialog.QieHuanSetDialog;
import com.wenhuitec.android.tang.ui.emu.DzkCodeTypeEmus;
import com.wenhuitec.android.tang.util.BytesUtil;
import com.wenhuitec.android.tang.util.CLibrary;
import com.wenhuitec.android.tang.util.ScanKeyManager;
import com.wenhuitec.android.tang.util.UsbPrinter;
import com.yzy.voice.VoicePlay;

import java.util.ArrayList;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class Activity1 extends HBaseActivity implements ViewTreeObserver.OnGlobalLayoutListener {

    private Context mContext;
    private BannerViewPager viewPager;
    private HBanner hBanner;
    private View rootView;
    public String[] NEEDED_PERMISSIONS = {
            Manifest.permission.READ_EXTERNAL_STORAGE,
            Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.INTERNET};


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
//        setContentView(R.layout.activity_main);
        rootView = LayoutInflater.from(this).inflate(R.layout.activity_main, null);
        setContentView(rootView);
        viewPager = findViewById(R.id.viewpager);
        rootView.getViewTreeObserver().addOnGlobalLayoutListener(this);
        mContext = this;
        viewPager = findViewById(R.id.viewpager);
        SharedPreferences sharedPreferences =
                getSharedPreferences("systemI", Context.MODE_PRIVATE); //私有数据
        String user = sharedPreferences.getString("user", "");
        if ("".equals(user)) {
            startActivity(new Intent(mContext, LoginActivity.class));
            finish();
            return;
        }
        SystemInit.getInstance(mContext);
        System.setProperty("jna.encoding", "gbk");
        findViewById(R.id.updateMenuBut).setOnClickListener(view -> {
            SystemInit.clear();
            SystemInit.getInstance(mContext);
        });
        findViewById(R.id.loginBut).setOnClickListener(view -> {
            SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
            editor.putString("user", "");
            editor.commit();
            startActivity(new Intent(mContext, LoginActivity.class));
            finish();
        });
        findViewById(R.id.printSetBut).setOnClickListener(view -> {
            PrintSetDialog printStart = new PrintSetDialog(mContext);
            printStart.show();
        });
        findViewById(R.id.qiehuanBut).setOnClickListener(view -> {
            QieHuanSetDialog qieHuanSetDialog = new QieHuanSetDialog(mContext);
            qieHuanSetDialog.show();

            qieHuanSetDialog.getZiXiaoPosEd().setText(sharedPreferences.getString("ziXiaoPos", ""));
            qieHuanSetDialog.getXiaoMaiBuPosEd().setText(sharedPreferences.getString("xiaoMaiBuPos", ""));

            qieHuanSetDialog.getYes().setOnClickListener(view1 -> {
                String xiaoMaiBuPos = qieHuanSetDialog.getXiaoMaiBuPosEd().getText().toString();
                String ziXiaoPos = qieHuanSetDialog.getZiXiaoPosEd().getText().toString();
                SharedPreferences.Editor editor = sharedPreferences.edit();//获取编辑器
                editor.putString("xiaoMaiBuPos", xiaoMaiBuPos);
                editor.putString("ziXiaoPos", ziXiaoPos);
                editor.commit();
                qieHuanSetDialog.dismiss();
            });
        });
        findViewById(R.id.connectPrintBut).setOnClickListener(view -> UsbPrinter.reloadClass(Activity1.this));
        findViewById(R.id.printTestBut).setOnClickListener(view -> {
            byte[] printByte = BytesUtil.printTest();
            if (printByte != null) {
                try {
                    UsbPrinter printer = UsbPrinter.getInstance(Activity1.this);
                    printer.write(printByte);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });

        findViewById(R.id.dzkCodeTypeBut).setOnClickListener(view -> {
            AlertDialog.Builder singleChoiceDialog = new AlertDialog.Builder(Activity1.this);
            singleChoiceDialog.setTitle("二维码类型");
            final Map<String, Integer> map = new LinkedHashMap<>();
            for (DzkCodeTypeEmus goo : DzkCodeTypeEmus.values()) {
                map.put(goo.getName(), goo.getValue());
            }
            Set<String> set = map.keySet();
            LinkedList<String> list = new LinkedList<>(set);
            final String[] keys = set.toArray(new String[]{});
            int type = SystemInit.getInstance(this).getDzkCodeType();
            DzkCodeTypeEmus emus = DzkCodeTypeEmus.getByValue(type);
            int index = -1;
            if(emus != null) {
                index = list.indexOf(emus.getName());
            }
            singleChoiceDialog.setSingleChoiceItems(keys, index, (dialog, which) -> {
                int state = map.get(keys[which]);
                SystemInit.getInstance(Activity1.this).setDzkCodeType(state);
            });
            //singleChoiceDialog.setPositiveButton("确定", (dialog, which) -> {});
            singleChoiceDialog.show();
        });
    }

    @Override
    public void onGlobalLayout() {
        rootView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
        verifyStoragePermissions(Activity1.this);
    }

    @Override
    void afterPermissions() {
        init();
    }

    @Override
    String[] needPermissions() {
        return NEEDED_PERMISSIONS;
    }


    private void init() {

        hBanner = HBanner.create(viewPager);
        List<SubView> data = new ArrayList<>();

        data.add(new ImageSubView.Builder(getBaseContext())
                .resId(R.drawable.banner1)
                .duration(5000)
                .build());
        data.add(new ImageSubView.Builder(getBaseContext())
                .resId(R.drawable.banner2)
                .duration(5000)
                .build());

        hBanner.sources(data);
        //设置viewpager切换方式
        viewPager.setPageTransformer(true, new DefaultTransformer());

        //开始显示或者自动播放
        hBanner.play(true);
        System.out.println("开始播放");
    }

    @Override
    protected void onResume() {
        if (hBanner != null)
            hBanner.play(true);
        super.onResume();
    }

    @Override
    protected void onPause() {
        if (hBanner != null)
            hBanner.pause(0);
        super.onPause();
    }


    @Override
    protected void onStop() {
        if (hBanner != null)
            hBanner = null;
        super.onStop();
    }

}